﻿@model IEnumerable<Common.Views.FaultsView>

@{
    ViewBag.Title = "Electros Ltd | Update Fault Status";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Update Fault Status</h2>

<script type="text/javascript">
    function updatefaults(counter) {
        if ($("#ddlFaults" + counter + "").val() == "") {
            alert("Choose a Fault Status!");
        }
        else {
            var order = $('#oid' + counter + '').val();
            var fault = $('#fid' + counter + '').val();
            var fsdetails = $('#fdetails' + counter + '').val();
            var faults = $('#ddlFaults' + counter + ' option:selected').val();

            var param = { oId: order, fId: fault, fDetails: fsdetails, sId: faults };
            $.ajax({
                url: '@Url.Action("UpdateFaultStatus")',
                type: "POST",
                datatype: "json",
                data: param,
                error: function (xmlHttpRequest, errorText, thrownError) {
                    alert(xmlHttpRequest + "|" + "An error has occured. " + "|" + thrownError);
                },
                success: function (data) {
                    alert("Fault Status has been successfully updated!");
                }
            });
        }
    }
</script>

@if (Model.Count() > 0)
{
    <div style="color:Red">
        Please note that only a test message is sent via SMS since credits will need to be purchased
    </div> <br />
    <span style="color: Red; font-size: 14px;" >
            @ViewBag.Message
    </span> <br />
    <table>
        <tr>
            <th>Username</th>
            <th>Product Name</th>
            <th>Product Image</th>
            <th>Fault Date</th>
            <th>Fault Details</th>
            <th>Fault Status</th>
            <th>Update Fault Status</th>      
        </tr>

    @{
        int counter = 1;
    }
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Username)
                @Html.HiddenFor(modelItem => item.OrderID, new { id = "oid" + counter + "" })
                @Html.HiddenFor(modelItem => item.FaultID, new { id = "fid" + counter + "" })
                @Html.HiddenFor(modelItem => item.FaultDetails, new { id = "fdetails" + counter + "" })
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ProductName)
            </td>
            <td>
                <img height="150px" width="150px" src= "@Url.Content(item.ImageLink)" alt="@Html.DisplayFor(modelItem => item.ImageLink)" />
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FaultDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FaultDetails)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FaultStatus)
            </td>
            <td>
                @Html.DropDownListFor(model => item.FaultID, (IEnumerable<SelectListItem>)ViewData["faultList"], "--Select Fault Status--", new { id = "ddlFaults" + counter + "" })
                <button type="button" onclick="updatefaults(@counter)">Update Fault Status</button>
            </td>
        </tr>
        { counter++; }
    }
    </table>
}
else
{
    <br /> <div style="color:Red">@Html.Label("No products were found")</div>
}